require "prefabutil"

local assets =
{
    Asset("ANIM", "anim/nuts_beebox.zip"),
}

local function updatelevel(inst)
    local lv = inst.components.harvestable.produce
    if lv < 1 then
        inst.AnimState:PlayAnimation("idle_0", true)
    elseif lv < 7 then
        inst.AnimState:PlayAnimation("idle_1", true)
    elseif lv < 15 then
        inst.AnimState:PlayAnimation("idle_2", true)
    else
        inst.AnimState:PlayAnimation("idle_3", true)
    end

    inst.AnimState:SetFrame(math.random(inst.AnimState:GetCurrentAnimationNumFrames()) - 1)
end

local function onhammered(inst, worker)
    if inst.components.burnable ~= nil and inst.components.burnable:IsBurning() then
        inst.components.burnable:Extinguish()
    end
    if inst.components.harvestable ~= nil then
        inst.components.harvestable:Harvest()
    end
    inst.components.lootdropper:DropLoot()
    local fx = SpawnPrefab("collapse_small")
    fx.Transform:SetPosition(inst.Transform:GetWorldPosition())
    fx:SetMaterial("wood")
    inst:Remove()
end

local function spwanloot(inst, picker, produce, lootname)
    for i = 1, produce, 1 do
        local loot = SpawnPrefab(lootname)
        if loot ~= nil then
            local stackable = loot.components.stackable
            if stackable then
                local this_stack_size = math.min(produce, loot.components.stackable.maxsize)
                produce = produce - this_stack_size
                loot.components.stackable:SetStackSize(this_stack_size)
            end
            if loot.components.inventoryitem ~= nil then
                loot.components.inventoryitem:InheritWorldWetnessAtTarget(inst)
            end
            if picker ~= nil and picker.components.inventory ~= nil then
                picker.components.inventory:GiveItem(loot, nil, inst:GetPosition())
            else
                LaunchAt(loot, inst, nil, 1, 1)
            end

            if not (produce > 0) then
                break
            end
        else
            break
        end
    end
end

local function onharvest(inst, picker, produce)
    -- if inst.components.harvestable then
    --     inst.components.harvestable:SetGrowTime(nil)
    --     inst.components.harvestable.pausetime = nil
    --     inst.components.harvestable:StopGrowing()
    -- end
    updatelevel(inst)
    if picker ~= nil and picker.components.inventory ~= nil then
        picker:PushEvent("harvestsomething", { object = inst })
    end

    -- （75%概率获得存储的蜂蜜总量的一半蜂蜜）
    -- 65%的概率额外掉一个松果，30%的概率额外掉一个桦栗果，5%的概率额外掉一个多枝树种
    spwanloot(inst, picker, produce * 2, "honey")
    spwanloot(inst, picker, produce * 2, "pinecone")
    if math.random() < 0.75 then
        spwanloot(inst, picker, produce, "honey")
    end
    if math.random() < 0.1 then
        if produce < 7 then
            spwanloot(inst, picker, 2, "butter")
        elseif produce < 15 then
            spwanloot(inst, picker, 3, "butter")
        else
            spwanloot(inst, picker, 4, "butter")
        end
    end
    local extra_loot_chance = math.random()
    if extra_loot_chance < 0.65 then
        spwanloot(inst, picker, 1, "pinecone")
    elseif extra_loot_chance < 0.95 then
        spwanloot(inst, picker, 1, "acorn")
    else
        spwanloot(inst, picker, 1, "twiggy_nut")
    end
end

local function fn()
    local inst = CreateEntity()

    inst.entity:AddTransform()
    inst.entity:AddAnimState()
    -- inst.entity:AddSoundEmitter()
    inst.entity:AddNetwork()

    inst:SetDeploySmartRadius(1.25) --recipe min_spacing/2
    MakeObstaclePhysics(inst, .5)

    inst.AnimState:SetBank("nuts_beebox")
    inst.AnimState:SetBuild("nuts_beebox")
    inst.AnimState:PlayAnimation("idle_0", true)

    inst:AddTag("structure")
    inst:AddTag("playerowned")
    inst:AddTag("beebox")
    -- inst:AddTag("beebeacon")

    inst.entity:SetPristine()

    if not TheWorld.ismastersim then
        return inst
    end

    inst:AddComponent("harvestable")
    inst.components.harvestable:SetUp(nil, TUNING.NUTS_BEEBOX_MAX_PRODUCE, TUNING.NUTS_BEEBOX_GROW_TIME, onharvest, updatelevel)

    inst:DoTaskInTime(0, updatelevel)

    inst:AddComponent("inspectable")
    inst:AddComponent("lootdropper")
    inst:AddComponent("workable")
    inst.components.workable:SetWorkAction(ACTIONS.HAMMER)
    inst.components.workable:SetWorkLeft(4)
    inst.components.workable:SetOnFinishCallback(onhammered)
    -- inst.components.workable:SetOnWorkCallback(onhit)

    MakeMediumBurnable(inst)
    MakeSmallPropagator(inst)

    return inst
end

return Prefab("nuts_beebox", fn, assets),
    MakePlacer("nuts_beebox_placer", "nuts_beebox", "nuts_beebox", "idle_0")
